package com.yowasa.xiuxian.engine.battle.strategy.skill.soild;

import com.google.common.collect.Maps;
import com.yowasa.xiuxian.annoations.BS;
import com.yowasa.xiuxian.constant.XiuxianConstants;
import com.yowasa.xiuxian.engine.battle.strategy.BaseDefendStrategy;
import com.yowasa.xiuxian.engine.battle.strategy.BattleStrategy;
import com.yowasa.xiuxian.entity.bo.battle.BattleContent;
import com.yowasa.xiuxian.entity.bo.battle.UserBattleContent;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.ElementEnum;
import com.yowasa.xiuxian.exception.HandlerException;
import lombok.Data;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Map;

@BS(name = "浊之躯")
@Order(1)
@Component
public class ZhuoZhiQu extends BaseDefendStrategy {
    @Override
    public ElementEnum getElement() {
        return ElementEnum.SOIL;
    }

    @Override
    public void limit(UserBO user) {
        if (user.getElement().get("土") < XiuxianConstants.DEFAULT_ELEMENT_ACTIVITY) {
            throw new HandlerException("学习/使用浊之躯必须具有土灵根");
        }
    }
    @Override
    public String limitDesc() {
        return "具有土灵根";
    }

    @Override
    public String desc() {
        return "对你造成最高伤害的技能对你造成的伤害减半,至多共减免双防合计*土灵根占比*20点伤害";
    }

    @Override
    public int defend(UserBattleContent user, UserBattleContent battleUser, BattleStrategy bs, int dmg, BattleContent content) {

        Info info = getInfo(user);
        Map.Entry<String, Integer> maxEntry = null;
        for (Map.Entry<String, Integer> entry : info.getRecord().entrySet()) {
            if (maxEntry == null || entry.getValue() > maxEntry.getValue()) {
                maxEntry = entry;
            }
        }
        String clazzName = bs.getClass().getName();
        Integer cNum = info.getRecord().get(clazzName);
        if (cNum == null) {
            info.getRecord().put(clazzName, dmg);
            return dmg;
        }
        if (maxEntry != null && maxEntry.getKey().equals(clazzName)) {
            int reduce = dmg / 2;
            int left = dmg - reduce;
            info.setReduce(info.getReduce() + reduce);
            info.getRecord().put(clazzName, info.getRecord().get(clazzName) + left);
            return left;
        } else {
            info.getRecord().put(clazzName, info.getRecord().get(clazzName) + dmg);
            return dmg;
        }


    }

    Info getInfo(UserBattleContent user) {
        Info info = (Info) user.getFlag().get("浊之躯");
        if (info == null) {
            info = new Info();
            Integer tu = user.getUserBO().getElement().get("土");
            info.setTotal((user.getBattleInfo().getDef() + user.getBattleInfo().getRes()) * tu / 15);
            user.getFlag().put("浊之躯", info);
        }
        return info;
    }

    @Override
    public boolean check(UserBattleContent user, BattleStrategy bs, BattleContent content) {
        Info info = getInfo(user);
        if (info.getReduce() >= info.getTotal()) {
            return false;
        }
        return true;
    }

    @Data
    private class Info {
        private int total;
        private int reduce = 0;
        private Map<String, Integer> record = Maps.newHashMap();

    }

}
